package com.zxx.autohomenavi.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.zxx.autohomenavi.page_com.CommonFragment2Activity;
import com.zxx.autohomenavi.page_com.CommonFragmentActivity;
import com.zxx.autohomenavi.page_json.MainActivity;
import com.zxx.autohomenavi.R;
import com.zxx.autohomenavi.base.BaseFragment;

import java.util.Date;
import java.util.Random;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.math.MathUtils;

/**
 * @author zxx on 2021/8/18
 */
public class TitleFragment extends BaseFragment {

    private TextView mTextView;

    public TitleFragment() {
        Bundle bundle = new Bundle();
        bundle.putString("title", "position");
        this.setArguments(bundle);
    }

    public static TitleFragment getInstance(String title) {
        TitleFragment fragment = new TitleFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_title, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        setOtherView(view);

        mTextView = view.findViewById(R.id.tv_title);
        final String title = getArguments() != null ? getArguments().getString("title", "") : "空";
        mTextView.setText(title);
        //不兼容就会换成sans字体
        //Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "assets/fonts/typeface.ttf");
        //mTextView.setTypeface(typeface);

        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (title) {
                    case "首页":
                        startActivity(new Intent(getContext(), MainActivity.class));
                        break;
                    case "找房":
                        startActivity(new Intent(getContext(), CommonFragmentActivity.class));
                        break;
                    case "我的":
                        startActivity(new Intent(getContext(), CommonFragment2Activity.class));
                        break;
                }
            }
        });
    }


    //------------------------GridView
    private String[] items = {"lorem", "aaaa", "vvvv", "ssss", "lozzzzrem", "cccc", "qqqq", "wwww", "eeee", "eeee",
            "ggggg", "eeee", "fffff", "ccccc", "vvvvv", "xxxx", "zzzz", "xxx", "ffff", "aaaa", "ssss", "dddd"};
    private void initView(View view) {
        GridView gridView = view.findViewById(R.id.grid_view);
        gridView.setAdapter(new GridAdapter(getContext(), android.R.layout.simple_list_item_1, items));

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mTextView.setText(items[position]);
            }
        });

        //预选
        final AutoCompleteTextView edit = view.findViewById(R.id.edit);
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mTextView.setText(edit.getText());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        edit.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_dropdown_item_1line, items));

        //翻转
        DigitalClock clock = view.findViewById(R.id.digital);
        final ViewFlipper flipper = view.findViewById(R.id.details);
        clock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipper.showNext();
            }
        });

        //自动翻转(循环的)切换方向是左右切换还是上下切换由你设置的动画决定。
        final ViewFlipper flipper2 = view.findViewById(R.id.flipper);
        flipper2.setInAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.push_right_in));
        flipper2.setOutAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.push_left_out));
        for (String item : items) {
            Button button = new Button(getContext());
            button.setText(item);

            flipper2.addView(button, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }
        flipper2.setFlipInterval(1000);
        flipper2.startFlipping();
    }

    class GridAdapter extends ArrayAdapter<String> {

        private Context context;
        public GridAdapter(@NonNull Context context, int resource, @NonNull String[] objects) {
            super(context, resource, objects);
            this.context = context;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder viewHolder = null;

            if (convertView == null) {
                convertView = new TextView(context);//或者LayoutInflate
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.textView.setText(items[position]);
            return convertView;
        }

        class ViewHolder {
            public TextView textView;
            public ViewHolder(View view) {
                textView = (TextView) view;//或者find
                textView.setBackgroundColor(Color.argb(255, new Random().nextInt(255), new Random().nextInt(255), (int) (1 + Math.random() * (10 - 1 + 1))));
                textView.setTextColor(Color.CYAN);
                textView.setGravity(Gravity.CENTER);
                //textView.setHeight(200);
            }
        }
    }


    //-----------------------otherView
    private void setOtherView(View view) {
        WebView webView = view.findViewById(R.id.webview);

        loadTime(webView);

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("Tag-->", "自己处理返回true，默认浏览器处理返回false：" + url);
                loadTime(view);
                return true;
            }
        });

        Handler handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {

                return false;
            }
        });
    }

    private void loadTime(WebView webView) {
        String page = "<html><body>" + "<a href=\"clock\">" + new Date().toString() + "</a></body></html>";
        webView.loadDataWithBaseURL("x-data://base", page, "text/html", "utf-8", null);
    }

}
